﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using System.Web.Security;

public partial class PersonTrainingExperience : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PersonHelper.BasicInformationJobIntentionCheck();

        if (!IsPostBack)
        {
            ddl_Year2.Items.Add(new ListItem("请选择", "0"));
            for (int _i = 2011; _i > 1940; _i--)
            {
                ddl_Year1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Year2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }

            ddl_Month2.Items.Add(new ListItem("请选择", "0"));
            for (int _i = 1; _i < 13; _i++)
            {
                ddl_Month1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Month2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }
            if (Membership.GetUser() != null)
            {
                var _t = TrainingExperienceManager.GetTrainingExperience(Membership.GetUser().UserName);
                ListView1.DataSource = _t;
                ListView1.DataBind();
            }
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
        var _t = TrainingExperienceManager.GetTrainingExperience(_id);
            if (_t != null)
            {
                txt_ID.Value = _t.ID.ToString();
                ddl_Year1.SelectedValue = _t.BeginDate.Year.ToString();
                ddl_Month1.SelectedValue = _t.BeginDate.Month.ToString();
                if (_t.OverDate != DateTime.MinValue)
                {
                    ddl_Year2.SelectedValue = _t.OverDate.Year.ToString();
                    ddl_Month2.SelectedValue = _t.OverDate.Month.ToString();
                }
                txt_Institytions.Text = _t.Institutions;
                ddl_Location.City = (int)_t.Location;
                txt_Course.Text = _t.Course;
                txt_Certificate.Text = _t.Certificate;
                DescriptionTextBox.Text = _t.Description;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            Button _btn = sender as Button;
            Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
            TrainingExperienceManager.Delete(_id);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_SaveNew_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void Save()
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(txt_ID.Value) ? Guid.Empty : new Guid(txt_ID.Value);
            var _user = Membership.GetUser();
            DateTime _beginDate = Convert.ToDateTime(ddl_Year1.SelectedValue + "/" + ddl_Month1.SelectedValue);
            //DateTime _overDate = DateTime.MinValue;
            if (ddl_Year2.SelectedValue != "0" || ddl_Month2.SelectedValue != "0") throw new Exception("请选择培训结束日期");
            DateTime _overDate = Convert.ToDateTime(ddl_Year2.SelectedValue + "/" + ddl_Month2.SelectedValue);

            TrainingExperienceManager.TrainingExperienceSave(_id,
                _user.UserName,
                txt_Course.Text,
                txt_Certificate.Text,
                _beginDate,
                _overDate, txt_Institytions.Text,
                Convert.ToInt32(ddl_Location.City),
                DescriptionTextBox.Text);

        }
        catch (Exception _e)
        {
            throw new Exception(_e.Message);
        }
    }
}
